/*
 * @Descripttion: 
 * @version: 
 * @Author: yuanliutech
 * @Date: 2023-01-05 17:01:05
 * @LastEditors: yuanliutech@lwj
 * @LastEditTime: 2023-01-05 20:56:50
 */
const app = getApp()
const chooseLocation = requirePlugin('chooseLocation');
import {
    showMsg
} from '../../../utils/config.js'
import {
    validateEmoji,
    validateSpecial
} from '../../../utils/validate.js'
import {
    reqOrgAdd,
    reqRegion,
    reqRegionLevel,
    reqOrgList
} from '../../../request/api'

var QQMapWX = require('../../../utils/qqmap-wx-jssdk.min'); //地址转经纬度
// 实例化API核心类
var qqmapsdk = new QQMapWX({ //地址转经纬度
    key: '6YRBZ-CE263-TES33-YGBKX-AJ2SH-I4FJH' // 必填,申请的key
})

Page({
    data: {
        bottomHeight: app.globalData.bottomHeight + 'px', //iphone底部距离
        navHeight: app.globalData.navHeight + 'px', //头部距离
        showhomeCountry: false,
        showType: false, // 机构属性
        showflatType: false, //是否展示单位类型弹窗
        shownature: false,
        showcategory: false,
        showgrade: false,
        flatTypeList: [{
                id: 1,
                text: '九小单位',
            }, {
                id: 2,
                text: '高危单位',
            }, {
                id: 3,
                text: '重点单位',
            }, {
                id: 4,
                text: '一般单位',
            }, {
                id: 5,
                text: '高层建筑',
            }, {
                id: 6,
                text: '其他单位',
            }, {
                id: 7,
                text: '超大综合体',
            }, {
                id: 8,
                text: '化工企业',
            }, {
                id: 9,
                text: '地下建筑',
            },
            // {
            //     id: 10,
            //     text: '地窖单位',
            // },
        ],
        natureList: [{
            id: 1,
            text: '国有企业',
        }, {
            id: 2,
            text: '集体企业',
        }, {
            id: 3,
            text: '民营企业',
        }, {
            id: 4,
            text: '个体工商户',
        }, {
            id: 5,
            text: '中外合资企业',
        }, {
            id: 6,
            text: '外商独资企业',
        }, {
            id: 7,
            text: '股份制企业',
        }, {
            id: 8,
            text: '事业单位',
        }, {
            id: 9,
            text: '国家单位',
        }, {
            id: 10,
            text: '照明场所',
        }, {
            id: 11,
            text: '其他',
        }],
        categoryList: [{
            id: 1,
            text: "医院"
        }, {
            id: 2,
            text: "科研单位"
        }, {
            id: 3,
            text: "住宅"
        }, {
            id: 4,
            text: "体育场"
        }, {
            id: 5,
            text: "工厂"
        }, {
            id: 6,
            text: "学校"
        }, {
            id: 7,
            text: "养老院"
        }, {
            id: 8,
            text: "政府机构"
        }, {
            id: 9,
            text: "车站"
        }, {
            id: 10,
            text: "码头"
        }, {
            id: 11,
            text: "企业"
        }, {
            id: 12,
            text: "商店"
        }, {
            id: 13,
            text: "宾馆"
        }, {
            id: 14,
            text: "其他"
        }, {
            id: 15,
            text: "非营利性机构"
        }, ],
        gradeList: [{
            id: 1,
            text: "一级消防安全重点单位"
        }, {
            id: 2,
            text: "二级消防安全重点单位"
        }, {
            id: 3,
            text: "三级消防安全重点单位"
        }, {
            id: 4,
            text: "非消防安全重点单位"
        }],
        form: {
            name: '',
            username: '',
            mobile: '',
            region: '',
            address: '',
            flatType: '', //单位类型
            flatTypeText: '',
            nature: "",
            natureText: "",
            category: "",
            categoryText: "",
            grade: "",
            gradeText: "",
            lng: '',
            lat: '',
            province: '',
            provinceCode: '',
            city: '',
            cityCode: '',
            area: '',
            areaCode: '',
            street: '',
            streetCode: '',
            construction: "", //施工单位
            homeCountry: "",
        },
        showprovince: false, //省弹窗
        showcity: false, //市弹窗
        showarea: false, //区弹窗
        showStreet: false, //是否展示街道弹窗
        showCommunity: false, //是否展示社区弹窗
        disabledBtn: false,
        // 地区选择开始
        showRegion: false,
        tabList: [{
            id: 0,
            name: '省',
        }, {
            id: 1,
            name: '市',
        }, {
            id: 2,
            name: '区',
        }, {
            id: 3,
            name: '街道',
        }, ],
        currentTab: 0,
        provinceCode: '',
        provinceName: '',
        cityList: [],
        cityCode: '',
        cityName: '',
        areaList: [],
        areaCode: '',
        areaName: '',
        provinceList: [], //省列表
        cityList: [], //市列表
        areaList: [], //区列表
        streetList: [],
        communityList: [], //社区列表
        streetCode: '',
        streetName: '',
        // 地区选择结束

        homeCountryList: [{
                "name": "China",
                "text": "中国"
            }, {
                "name": "Afghanistan",
                "text": "阿富汗"
            },
            {
                "name": "Aland Islands",
                "text": "奥兰群岛"
            },
            {
                "name": "Albania",
                "text": "阿尔巴尼亚"
            },
            {
                "name": "Algeria",
                "text": "阿尔及利亚"
            },
            {
                "name": "American Samoa",
                "text": "美属萨摩亚"
            },
            {
                "name": "Andorra",
                "text": "安道尔"
            },
            {
                "name": "Angola",
                "text": "安哥拉"
            },
            {
                "name": "Anguilla",
                "text": "安圭拉"
            },
            {
                "name": "Antigua and Barbuda",
                "text": "安提瓜和巴布达"
            },
            {
                "name": "Argentina",
                "text": "阿根廷"
            },
            {
                "name": "Armenia",
                "text": "亚美尼亚"
            },
            {
                "name": "Aruba",
                "text": "阿鲁巴"
            },
            {
                "name": "Australia",
                "text": "澳大利亚"
            },
            {
                "name": "Austria",
                "text": "奥地利"
            },
            {
                "name": "Azerbaijan",
                "text": "阿塞拜疆"
            },
            {
                "name": "Bangladesh",
                "text": "孟加拉"
            },
            {
                "name": "Bahrain",
                "text": "巴林"
            },
            {
                "name": "Bahamas",
                "text": "巴哈马"
            },
            {
                "name": "Barbados",
                "text": "巴巴多斯"
            },
            {
                "name": "Belarus",
                "text": "白俄罗斯"
            },
            {
                "name": "Belgium",
                "text": "比利时"
            },
            {
                "name": "Belize",
                "text": "伯利兹"
            },
            {
                "name": "Benin",
                "text": "贝宁"
            },
            {
                "name": "Bermuda",
                "text": "百慕大"
            },
            {
                "name": "Bhutan",
                "text": "不丹"
            },
            {
                "name": "Bolivia",
                "text": "玻利维亚"
            },
            {
                "name": "Bosnia and Herzegovina",
                "text": "波斯尼亚和黑塞哥维那"
            },
            {
                "name": "Botswana",
                "text": "博茨瓦纳"
            },
            {
                "name": "Bouvet Island",
                "text": "布维岛"
            },
            {
                "name": "Brazil",
                "text": "巴西"
            },
            {
                "name": "Brunei",
                "text": "文莱"
            },
            {
                "name": "Bulgaria",
                "text": "保加利亚"
            },
            {
                "name": "Burkina Faso",
                "text": "布基纳法索"
            },
            {
                "name": "Burundi",
                "text": "布隆迪"
            },
            {
                "name": "Cambodia",
                "text": "柬埔寨"
            },
            {
                "name": "Cameroon",
                "text": "喀麦隆"
            },
            {
                "name": "Canada",
                "text": "加拿大"
            },
            {
                "name": "Cape Verde",
                "text": "佛得角"
            },
            {
                "name": "Central African Republic",
                "text": "中非"
            },
            {
                "name": "Chad",
                "text": "乍得"
            },
            {
                "name": "Chile",
                "text": "智利"
            },
            {
                "name": "Christmas Islands",
                "text": "圣诞岛"
            },
            {
                "name": "Cocos (keeling) Islands",
                "text": "科科斯（基林）群岛"
            },
            {
                "name": "Colombia",
                "text": "哥伦比亚"
            },
            {
                "name": "Comoros",
                "text": "科摩罗"
            },
            {
                "name": "Congo (Congo-Kinshasa)",
                "text": "刚果（金）"
            },
            {
                "name": "Congo",
                "text": "刚果"
            },
            {
                "name": "Cook Islands",
                "text": "库克群岛"
            },
            {
                "name": "Costa Rica",
                "text": "哥斯达黎加"
            },
            {
                "name": "Cote D’Ivoire",
                "text": "科特迪瓦"
            },

            {
                "name": "Croatia",
                "text": "克罗地亚"
            },
            {
                "name": "Cuba",
                "text": "古巴"
            },
            {
                "name": "Czech",
                "text": "捷克"
            },
            {
                "name": "Cyprus",
                "text": "塞浦路斯"
            },
            {
                "name": "Denmark",
                "text": "丹麦"
            },
            {
                "name": "Djibouti",
                "text": "吉布提"
            },
            {
                "name": "Dominica",
                "text": "多米尼加"
            },
            {
                "name": "East Timor",
                "text": "东帝汶"
            },
            {
                "name": "Ecuador",
                "text": "厄瓜多尔"
            },
            {
                "name": "Egypt",
                "text": "埃及"
            },
            {
                "name": "Equatorial Guinea",
                "text": "赤道几内亚"
            },
            {
                "name": "Eritrea",
                "text": "厄立特里亚"
            },
            {
                "name": "Estonia",
                "text": "爱沙尼亚"
            },
            {
                "name": "Ethiopia",
                "text": "埃塞俄比亚"
            },
            {
                "name": "Faroe Islands",
                "text": "法罗群岛"
            },
            {
                "name": "Fiji",
                "text": "斐济"
            },
            {
                "name": "Finland",
                "text": "Finland"
            },
            {
                "name": "France",
                "text": "法国"
            },
            {
                "name": "Franch Metropolitan",
                "text": "法国大都会"
            },
            {
                "name": "Franch Guiana",
                "text": "法属圭亚那"
            },
            {
                "name": "French Polynesia",
                "text": "法属波利尼西亚"
            },
            {
                "name": "Gabon",
                "text": "加蓬"
            },
            {
                "name": "Gambia",
                "text": "冈比亚"
            },
            {
                "name": "Georgia",
                "text": "格鲁吉亚"
            },
            {
                "name": "Germany",
                "text": "德国"
            },
            {
                "name": "Ghana",
                "text": "加纳"
            },
            {
                "name": "Gibraltar",
                "text": "直布罗陀"
            },
            {
                "name": "Greece",
                "text": "希腊"
            },
            {
                "name": "Grenada",
                "text": "格林纳达"
            },
            {
                "name": "Guadeloupe",
                "text": "瓜德罗普岛"
            },
            {
                "name": "Guam",
                "text": "关岛"
            },
            {
                "name": "Guatemala",
                "text": "危地马拉"
            },
            {
                "name": "Guernsey",
                "text": "根西岛"
            },
            {
                "name": "Guinea-Bissau",
                "text": "几内亚比绍"
            },
            {
                "name": "Guinea",
                "text": "几内亚"
            },
            {
                "name": "Guyana",
                "text": "圭亚那"
            },
            {
                "name": "Hong Kong",
                "text": "香港 （中国）"
            },
            {
                "name": "Haiti",
                "text": "海地"
            },
            {
                "name": "Honduras",
                "text": "洪都拉斯"
            },
            {
                "name": "Hungary",
                "text": "匈牙利"
            },
            {
                "name": "Iceland",
                "text": "冰岛"
            },
            {
                "name": "India",
                "text": "印度"
            },
            {
                "name": "Indonesia",
                "text": "印度尼西亚"
            },
            {
                "name": "Iran",
                "text": "伊朗"
            },
            {
                "name": "Iraq",
                "text": "伊拉克"
            },
            {
                "name": "Ireland",
                "text": "爱尔兰"
            },
            {
                "name": "Isle of Man",
                "text": "马恩岛"
            },
            {
                "name": "Israel",
                "text": "以色列"
            },
            {
                "name": "Italy",
                "text": "意大利"
            },
            {
                "name": "Jamaica",
                "text": "牙买加"
            },
            {
                "name": "Japan",
                "text": "日本"
            },
            {
                "name": "Jersey",
                "text": "泽西岛"
            },
            {
                "name": "Jordan",
                "text": "约旦"
            },
            {
                "name": "Kazakhstan",
                "text": "哈萨克斯坦"
            },
            {
                "name": "Kenya",
                "text": "肯尼亚"
            },
            {
                "name": "Kiribati",
                "text": "基里巴斯"
            },
            {
                "name": "Korea (South)",
                "text": "韩国"
            },
            {
                "name": "Korea (North)",
                "text": "朝鲜"
            },
            {
                "name": "Kuwait",
                "text": "科威特"
            },
            {
                "name": "Kyrgyzstan",
                "text": "吉尔吉斯斯坦"
            },
            {
                "name": "Laos",
                "text": "老挝"
            },
            {
                "name": "Latvia",
                "text": "拉脱维亚"
            },
            {
                "name": "Lebanon",
                "text": "黎巴嫩"
            },
            {
                "name": "Lesotho",
                "text": "莱索托"
            },
            {
                "name": "Liberia",
                "text": "利比里亚"
            },
            {
                "name": "Libya",
                "text": "利比亚"
            },
            {
                "name": "Liechtenstein",
                "text": "列支敦士登"
            },
            {
                "name": "Lithuania",
                "text": "立陶宛"
            },
            {
                "name": "Luxembourg",
                "text": "卢森堡"
            },
            {
                "name": "Macau",
                "text": "澳门（中国）"
            },
            {
                "name": "Macedonia",
                "text": "马其顿"
            },
            {
                "name": "Malawi",
                "text": "马拉维"
            },
            {
                "name": "Malaysia",
                "text": "马来西亚"
            },
            {
                "name": "Madagascar",
                "text": "马达加斯加"
            },
            {
                "name": "Maldives",
                "text": "马尔代夫"
            },
            {
                "name": "Mali",
                "text": "马里"
            },
            {
                "name": "Malta",
                "text": "马耳他"
            },
            {
                "name": "Marshall Islands",
                "text": "马绍尔群岛"
            },
            {
                "name": "Martinique",
                "text": "马提尼克岛"
            },
            {
                "name": "Mauritania",
                "text": "毛里塔尼亚"
            },
            {
                "name": "Mauritius",
                "text": "毛里求斯"
            },
            {
                "name": "Mayotte",
                "text": "马约特"
            },
            {
                "name": "Mexico",
                "text": "墨西哥"
            },
            {
                "name": "Micronesia",
                "text": "密克罗尼西亚"
            },
            {
                "name": "Moldova",
                "text": "摩尔多瓦"
            },
            {
                "name": "Monaco",
                "text": "摩纳哥"
            },
            {
                "name": "Mongolia",
                "text": "蒙古"
            },
            {
                "name": "Montenegro",
                "text": "黑山"
            },
            {
                "name": "Montserrat",
                "text": "蒙特塞拉特"
            },
            {
                "name": "Morocco",
                "text": "摩洛哥"
            },
            {
                "name": "Mozambique",
                "text": "莫桑比克"
            },
            {
                "name": "Myanmar",
                "text": "缅甸"
            },
            {
                "name": "Namibia",
                "text": "纳米比亚"
            },
            {
                "name": "Nauru",
                "text": "瑙鲁"
            },
            {
                "name": "Nepal",
                "text": "尼泊尔"
            },
            {
                "name": "Netherlands",
                "text": "荷兰"
            },
            {
                "name": "New Caledonia",
                "text": "新喀里多尼亚"
            },
            {
                "name": "New Zealand",
                "text": "新西兰"
            },
            {
                "name": "Nicaragua",
                "text": "尼加拉瓜"
            },
            {
                "name": "Niger",
                "text": "尼日尔"
            },
            {
                "name": "Nigeria",
                "text": "尼日利亚"
            },
            {
                "name": "Niue",
                "text": "纽埃"
            },
            {
                "name": "Norfolk Island",
                "text": "诺福克岛"
            },
            {
                "name": "Norway",
                "text": "挪威"
            },
            {
                "name": "Oman",
                "text": "阿曼"
            },
            {
                "name": "Pakistan",
                "text": "巴基斯坦"
            },
            {
                "name": "Palau",
                "text": "帕劳"
            },
            {
                "name": "Palestine",
                "text": "巴勒斯坦"
            },
            {
                "name": "Panama",
                "text": "巴拿马"
            },
            {
                "name": "Papua New Guinea",
                "text": "巴布亚新几内亚"
            },
            {
                "name": "Paraguay",
                "text": "巴拉圭"
            },
            {
                "name": "Peru",
                "text": "秘鲁"
            },
            {
                "name": "Philippines",
                "text": "菲律宾"
            },
            {
                "name": "Pitcairn Islands",
                "text": "皮特凯恩群岛"
            },
            {
                "name": "Poland",
                "text": "波兰"
            },
            {
                "name": "Portugal",
                "text": "葡萄牙"
            },
            {
                "name": "Puerto Rico",
                "text": "波多黎各"
            },
            {
                "name": "Qatar",
                "text": "卡塔尔"
            },
            {
                "name": "Reunion",
                "text": "留尼汪岛"
            },
            {
                "name": "Romania",
                "text": "罗马尼亚"
            },
            {
                "name": "Rwanda",
                "text": "卢旺达"
            },
            {
                "name": "Russian Federation",
                "text": "俄罗斯联邦"
            },
            {
                "name": "Saint Helena",
                "text": "圣赫勒拿"
            },
            {
                "name": "Saint Kitts-Nevis",
                "text": "圣基茨和尼维斯"
            },
            {
                "name": "Saint Lucia",
                "text": "圣卢西亚"
            },
            {
                "name": "Saint Vincent and the Grenadines",
                "text": "圣文森特和格林纳丁斯"
            },
            {
                "name": "El Salvador",
                "text": "萨尔瓦多"
            },
            {
                "name": "Samoa",
                "text": "萨摩亚"
            },
            {
                "name": "San Marino",
                "text": "圣马力诺"
            },
            {
                "name": "Sao Tome and Principe",
                "text": "圣多美和普林西比"
            },
            {
                "name": "Saudi Arabia",
                "text": "沙特阿拉伯"
            },
            {
                "name": "Senegal",
                "text": "塞内加尔"
            },
            {
                "name": "Seychelles",
                "text": "塞舌尔"
            },
            {
                "name": "Sierra Leone",
                "text": "塞拉利昂"
            },
            {
                "name": "Singapore",
                "text": "新加坡"
            },
            {
                "name": "Serbia",
                "text": "塞尔维亚"
            },
            {
                "name": "Slovakia",
                "text": "斯洛伐克"
            },
            {
                "name": "Slovenia",
                "text": "斯洛文尼亚"
            },
            {
                "name": "Solomon Islands",
                "text": "所罗门群岛"
            },
            {
                "name": "Somalia",
                "text": "索马里"
            },
            {
                "name": "South Africa",
                "text": "南非"
            },
            {
                "name": "Spain",
                "text": "西班牙"
            },
            {
                "name": "Sri Lanka",
                "text": "斯里兰卡"
            },
            {
                "name": "Sudan",
                "text": "苏丹"
            },
            {
                "name": "Suriname",
                "text": "苏里南"
            },
            {
                "name": "Swaziland",
                "text": "斯威士兰"
            },
            {
                "name": "Sweden",
                "text": "瑞典"
            },
            {
                "name": "Switzerland",
                "text": "瑞士"
            },
            {
                "name": "Syria",
                "text": "叙利亚"
            },
            {
                "name": "Tajikistan",
                "text": "塔吉克斯坦"
            },
            {
                "name": "Tanzania",
                "text": "坦桑尼亚"
            },
            {
                "name": "Taiwan",
                "text": "台湾 （中国）"
            },
            {
                "name": "Thailand",
                "text": "泰国"
            },
            {
                "name": "Trinidad and Tobago",
                "text": "特立尼达和多巴哥"
            },
            {
                "name": "Timor-Leste",
                "text": "东帝汶"
            },
            {
                "name": "Togo",
                "text": "多哥"
            },
            {
                "name": "Tokelau",
                "text": "托克劳"
            },
            {
                "name": "Tonga",
                "text": "汤加"
            },
            {
                "name": "Tunisia",
                "text": "突尼斯"
            },
            {
                "name": "Turkey",
                "text": "土耳其"
            },
            {
                "name": "Turkmenistan",
                "text": "土库曼斯坦"
            },
            {
                "name": "Tuvalu",
                "text": "图瓦卢"
            },
            {
                "name": "Uganda",
                "text": "乌干达"
            },
            {
                "name": "Ukraine",
                "text": "乌克兰"
            },
            {
                "name": "United Arab Emirates",
                "text": "阿拉伯联合酋长国"
            },
            {
                "name": "United Kingdom",
                "text": "英国"
            },
            {
                "name": "United States",
                "text": "美国"
            },
            {
                "name": "Uruguay",
                "text": "乌拉圭"
            },
            {
                "name": "Uzbekistan",
                "text": "乌兹别克斯坦"
            },
            {
                "name": "Vanuatu",
                "text": "瓦努阿图"
            },
            {
                "name": "Vatican City",
                "text": "梵蒂冈"
            },
            {
                "name": "Venezuela",
                "text": "委内瑞拉"
            },
            {
                "name": "Vietnam",
                "text": "越南"
            },
            {
                "name": "Wallis and Futuna",
                "text": "瓦利斯群岛和富图纳群岛"
            },
            {
                "name": "Western Sahara",
                "text": "西撒哈拉"
            },
            {
                "name": "Yemen",
                "text": "也门"
            },
            {
                "name": "Yugoslavia",
                "text": "南斯拉夫"
            },
            {
                "name": "Zambia",
                "text": "赞比亚"
            },
            {
                "name": "Zimbabwe",
                "text": "津巴布韦"
            }
        ],
        typeList: [{
                id: 1,
                text: "联网单位"
            }, {
                id: 2,
                text: "监管单位"
            },
            // {
            //     id: 3,
            //     text: "维保单位"
            // }, 
            {
                id: 4,
                text: "代理商"
            }, {
                id: 5,
                text: "工程商"
            }, {
                id: 6,
                text: "经销商"
            }
        ]
    },
    onShow() {
        this.getRegion()
        this.getLocation() //获取地图地址信息
    },

    swichTab(e) {
        let {
            id
        } = e.currentTarget.dataset
        this.setData({
            currentTab: id
        })
    },

    bindChangeTab(e) {
        let {
            current
        } = e.detail
        this.setData({
            currentTab: current
        })
    },

    // 获取名称
    nameChange(e) {
        this.data.form.name = validateEmoji(e.detail.value.replace(/\s+/g, ''))
        this.setData({
            "form.name": this.data.form.name
        })
    },
    // 清除名称
    clearName() {
        this.setData({
            "form.name": ''
        })
    },
    // 施工单位
    constructionChange(e) {
        this.data.form.construction = validateEmoji(e.detail.value.replace(/\s+/g, ''))
        this.setData({
            "form.construction": this.data.form.construction
        })
    },
    clearConstruction() {
        this.setData({
            "form.construction": ''
        })
    },
    usernameChange(e) {
        this.data.form.username = validateEmoji(e.detail.value.replace(/\s+/g, ''))
        this.setData({
            "form.username": this.data.form.username
        })
    },
    clearUsername() {
        this.setData({
            "form.username": ''
        })
    },
    mobileChange(e) {
        this.data.form.mobile = validateEmoji(e.detail.value.replace(/\s+/g, ''))
        this.setData({
            "form.mobile": this.data.form.mobile
        })
    },
    clearMobile() {
        this.setData({
            "form.mobile": ''
        })
    },
    addressChange(e) {
        this.data.form.address = validateEmoji(e.detail.value.replace(/\s+/g, ''))
        this.setData({
            "form.address": this.data.form.address
        })
    },
    clearAddress() {
        this.setData({
            "form.address": ''
        })
    },

    // 弹出省市区街道
    showRegionClick() {
        this.setData({
            showRegion: true
        })
    },
    closeRegion() {
        this.setData({
            showRegion: false
        })
    },
    // 获取省级数据
    async getRegion() {
        const res = await reqRegion()
        if (res.code !== 20000) return showMsg(res.message)
        console.log(res);
        let {
            rows
        } = res.data
        this.setData({
            provinceList: rows
        })
    },
    // 点击省
    async provinceClick(e) {
        this.setData({
            cityList: [],
            areaList: [],
            streetList: [],
            cityCode: "",
            areaCode: "",
            streetCode: "",
        })
        let {
            item
        } = e.currentTarget.dataset
        const res = await reqRegionLevel(item.regionCode)
        if (res.code !== 20000) return showMsg(res.message)
        let {
            rows
        } = res.data
        console.log(rows);
        this.setData({
            provinceCode: item.regionCode,
            provinceName: item.regionName,
            cityList: rows,
            currentTab: 1,
        })
    },
    // 点击市
    async cityClick(e) {
        this.setData({
            areaList: [],
            streetList: [],
            areaCode: "",
            streetCode: "",
        })
        let {
            item
        } = e.currentTarget.dataset
        const res = await reqRegionLevel(item.regionCode)
        if (res.code !== 20000) return showMsg(res.message)
        let {
            rows
        } = res.data
        this.setData({
            cityCode: item.regionCode,
            cityName: item.regionName,
            areaList: rows,
            currentTab: 2,
        })
    },
    // 点击区
    async areaClick(e) {
        this.setData({
            streetList: [],
            streetCode: "",
        })
        let {
            item
        } = e.currentTarget.dataset
        const res = await reqRegionLevel(item.regionCode)
        if (res.code !== 20000) return showMsg(res.message)
        let {
            rows
        } = res.data
        console.log(rows);
        this.setData({
            areaCode: item.regionCode,
            areaName: item.regionName,
            streetList: rows,
            currentTab: 3,
        })
    },
    // 点击街道
    async streetClick(e) {
        let {
            item
        } = e.currentTarget.dataset
        this.setData({
            streetCode: item.regionCode,
            streetName: item.regionName,
        })
    },
    // 确定
    confirmRegion() {
        let {
            streetCode,
            provinceName,
            cityName,
            areaName,
            streetName,
        } = this.data
        if (!streetCode) return showMsg("请选择到街道")
        this.setData({
            'form.region': provinceName + cityName + areaName + streetName
        })
        this.closeRegion()
    },

    //点击腾讯地图选点获取位置
    // getLocationClick() {
    //     wx.showLoading({
    //         title: '加载中'
    //     })
    //     // wgs84   gcj02
    //     wx.getLocation({
    //         type: 'wgs84',
    //         isHighAccuracy: false, //开启高精度定位
    //         success(res) {
    //             wx.hideLoading()
    //             const key = '44FBZ-OVKKP-T23D5-VGHUE-XDXC3-BDF53'; //使用在腾讯位置服务申请的key
    //             const referer = '精卫安'; //调用插件的app的名称
    //             const location = JSON.stringify({
    //                 latitude: res.latitude,
    //                 longitude: res.longitude
    //             });
    //             const category = '生活服务,娱乐休闲，公司企业';
    //             wx.navigateTo({
    //                 url: `plugin://chooseLocation/index?key=${key}&referer=${referer}&location=${location}&category=${category}`
    //             });
    //         },
    //         fail(err) {
    //             console.log(err);
    //             wx.showToast({
    //                 title: '定位失败,请在微信小程序设置中>位置信息>打开定位功能',
    //                 icon: 'none',
    //                 duration: 3000
    //             })
    //         }
    //     })
    // },

    // 地址转经纬度
    geocoder(address) {
        console.log(address);
        //调用地址解析接口
        qqmapsdk.geocoder({
            //获取表单传入地址
            address: address, //地址
            success: (res) => { //成功后的回调
                console.log(res.result.location.lng, '成功后的回调')
                console.log(res.result.location.lat, '成功后的回调')
                this.setData({
                    "form.lng": res.result.location.lng,
                    "form.lat": res.result.location.lat,
                })
            },
            fail: function (error) {
                console.error(error, 'error的回调');
            },
            complete: function (res) {
                console.log(res, 'complete的回调');
            }
        })
    },

    async confirm() {
        if (this.data.disabledBtn) return
        let {
            name,
            username,
            mobile,
            address,
            flatType,
            province,
            provinceCode,
            city,
            cityCode,
            area,
            areaCode,
            street,
            streetCode,
            community,
            communityCode,
            lng,
            lat,
            construction,
            homeCountry,
            nature,
            category,
            grade,
            type,
        } = this.data.form
        // let {
        //     // provinceName,
        //     // provinceCode,
        //     // cityName,
        //     // cityCode,
        //     // areaName,
        //     // areaCode,
        //     // streetName,
        //     // streetCode,
        // } = this.data
        if (!(name)) return showMsg("请输入名称")
        if (!(type)) return showMsg("请选择团队属性")
        // if (!(homeCountry)) return showMsg("请输入所属国家")
        if (!(areaCode)) return showMsg("请选择地区")
        if (!(province)) return showMsg("请选择省")
        if (!(city)) return showMsg("请选择市")
        if (!(area)) return showMsg("请选择区")
        if (!(street)) return showMsg("请选择街道")
        if (!(address)) return showMsg("请输入详细地址")
        // if (this.data.form.type == 1) {
        //     if (!(flatType)) return showMsg("请选择单位监管类型")
        //     if (!(nature)) return showMsg("请选择单位性质")
        //     if (!(category)) return showMsg("请选择单位类别")
        //     if (!(grade)) return showMsg("请选择监管等级")
        // }
        if (!validateSpecial(name)) return

        await this.geocoder(this.data.form.province + this.data.form.city + this.data.form.area + this.data.form.street)

        let params = {
            userId: app.globalData.userId,
            orgName: name,
            username,
            mobile,
            address,
            // province: provinceName,
            // provinceCode,
            // city: cityName,
            // cityCode,
            // area: areaName,
            // areaCode,
            // street: streetName,
            // streetCode,
            flatType,
            province,
            provinceCode,
            city,
            cityCode,
            area,
            areaCode,
            street,
            streetCode,
            community,
            communityCode,
            lng,
            lat,
            construction,
            homeCountry,
            nature,
            category,
            grade,
            type
        }
        this.reqAddFun(params)
    },
    async reqAddFun(params) {
        if (this.data.form.type != 1) {
            this.data.form.flatType = ""
            this.data.form.nature = ""
            this.data.form.category = ""
            this.data.form.grade = ""
        }
        const res = await reqOrgAdd(params)
        if (res.code != 20000) return showMsg(res.message)
        showMsg("创建成功", "success")
        this.setData({
            disabledBtn: true
        })
        // setTimeout(() => {
        //     wx.switchTab({
        //         url: '/pages/index/index',
        //     })
        // }, 1000)
        this.getOrgList()
    },
    async getOrgList() {
        let params = {
            tenantId: '0',
            userId: app.globalData.userId,
            roleName: app.globalData.rolename,
        }
        const res = await reqOrgList(1, 10, params)
        if (res.code !== 20000) return showMsg(res.message)
        let {
            rows
        } = res.data
        app.globalData.orgId = rows[0].id
        wx.setStorageSync('orgId', rows[0].id);
        app.globalData.orgName = rows[0].orgName
        wx.setStorageSync('orgName', rows[0].orgName);
        app.globalData.isTeamManager = rows[0].role === 1 ? true : false //role为1：单位管理员 role为2：消防安全员
        wx.setStorageSync('isTeamManager', rows[0].role === 1 ? true : false);
        wx.switchTab({
            url: '/pages/index/index',
        })
    },

    // 隐藏所属国家
    homeCountryClose() {
        this.setData({
            showhomeCountry: false
        })
    },

    // 弹出所属国家
    clickhomeCountry() {
        this.setData({
            showhomeCountry: true
        })
    },

    // 隐藏机构属性
    typeClose() {
        this.setData({
            showType: false
        })
    },

    // 隐藏单位类型
    flatTypeClose() {
        this.setData({
            showflatType: false
        })
    },

    // 弹出单位类型
    clickBuildingType() {
        this.setData({
            showflatType: true
        })
    },

    // 机构属性
    clickflatType() {
        this.setData({
            showType: true
        })
    },

    // 弹出单位性质
    clicknatureType() {
        this.setData({
            shownature: true
        })
    },
    // 隐藏单位性质
    natureClose() {
        this.setData({
            shownature: false
        })
    },
    // 确定单位性质
    natureConfirm(e) {
        const {
            value,
            index
        } = e.detail;
        this.setData({
            "form.nature": value.id,
            "form.natureText": value.text,
            shownature: false,
        })
    },

    // 弹出单位类别
    clickcategory() {
        this.setData({
            showcategory: true
        })
    },
    // 隐藏单位类别
    categoryClose() {
        this.setData({
            showcategory: false
        })
    },
    // 确定单位类别
    categoryConfirm(e) {
        const {
            value,
            index
        } = e.detail;
        this.setData({
            "form.category": value.id,
            "form.categoryText": value.text,
            showcategory: false,
        })
    },

    // 弹出监管等级
    clickgrade() {
        this.setData({
            showgrade: true
        })
    },
    // 隐藏监管等级
    gradeClose() {
        this.setData({
            showgrade: false
        })
    },
    // 确定监管等级
    gradeConfirm(e) {
        const {
            value,
            index
        } = e.detail;
        this.setData({
            "form.grade": value.id,
            "form.gradeText": value.text,
            showgrade: false,
        })
    },

    homeCountryConfirm(e) {
        const {
            value,
            index
        } = e.detail;
        console.log(e);
        this.setData({
            "form.homeCountry": value.text,
            showhomeCountry: false,
        })
    },

    // 确定建筑物性质
    flatTypeConfirm(e) {
        const {
            value,
            index
        } = e.detail;
        this.setData({
            "form.flatType": value.id,
            "form.flatTypeText": value.text,
            showflatType: false,
        })
    },

    // 确定机构属性
    typeConfirm(e) {
        const {
            value,
            index
        } = e.detail;
        this.setData({
            "form.type": value.id,
            "form.typeText": value.text,
            showType: false,
        })
    },

    //点击腾讯地图选点获取位置
    getLocationClick() {
        wx.showLoading({
            title: '加载中',
            mask: true,
        })
        // wgs84   gcj02
        wx.getLocation({
            type: 'wgs84',
            isHighAccuracy: false, //开启高精度定位
            success(res) {
                wx.hideLoading()
                const key = '44FBZ-OVKKP-T23D5-VGHUE-XDXC3-BDF53'; //使用在腾讯位置服务申请的key
                const referer = '精卫安'; //调用插件的app的名称
                const location = JSON.stringify({
                    latitude: res.latitude,
                    longitude: res.longitude
                });
                const category = '生活服务,娱乐休闲，公司企业';
                wx.navigateTo({
                    url: `plugin://chooseLocation/index?key=${key}&referer=${referer}&location=${location}&category=${category}`
                });
            },
            fail(err) {
                console.log(err);
                wx.showToast({
                    title: '定位失败,请在微信小程序设置中>位置信息>打开定位功能',
                    icon: 'none',
                    duration: 3000
                })
            }
        })
    },

    // 依据腾讯地图选点获取位置
    getLocation() {
        const location = chooseLocation.getLocation(); // 如果点击确认选点按钮，则返回选点结果对象，否则返回null
        if (location !== null) {
            console.log(location);
            this.setData({
                // "form.lng": location.longitude,
                // "form.lat": location.latitude,
                "form.region": location.province + location.city + location.district,
                "form.province": location.province || location.name, //省
                "form.city": location.city, //市
                "form.area": location.district, //区
                "form.address": location.name,
            })
            this.getProvince(location)
        }
    },
    // 获取省级编码
    async getProvince(location) {
        wx.showLoading({
            title: '加载中',
            mask: true,
        })

        const res = await reqRegion()
        if (res.code !== 20000) return showMsg(res.message)
        let {
            rows
        } = res.data
        let data = ''
        rows.forEach(item => {
            if (item.regionName === this.data.form.province) {
                data = item
            }
        })
        this.setData({
            'form.provinceCode': data.regionCode
        })
        this.getCity(location, data.regionCode)
    },
    // 获取市级编码
    async getCity(location, code) {
        const res = await reqRegionLevel(code)
        if (res.code !== 20000) return showMsg(res.message)
        let {
            rows
        } = res.data
        let data = ''
        this.setData({
            cityList: rows
        })
        console.log(rows);
        rows.forEach(item => {
            if (item.regionName === location.city) {
                data = item
            }
        })
        // 市辖区查不到  默认选择第一个  
        if (data == "") {
            this.setData({
                'form.city': rows[0].regionName,
                'form.cityCode': rows[0].regionCode,
                // 'form.street': ""
            })
            // wx.hideLoading()
            // return;
        } else {
            this.setData({
                'form.cityCode': data.regionCode
            })
        }
        this.getArea(location, data.regionCode || rows[0].regionCode) // 获取区级编码
    },
    // 获取区级编码
    async getArea(location, code) {
        const res = await reqRegionLevel(code)
        if (res.code !== 20000) return showMsg(res.message)
        let {
            rows
        } = res.data
        let data = ''
        console.log(rows);
        this.setData({
            areaList: rows
        })
        rows.forEach(item => {
            if (item.regionName === location.district) {
                data = item
            }
        })
        // 市辖区查不到  清空  
        if (data == "") {
            this.setData({
                'form.area': "",
                'form.street': ""
            })
            wx.hideLoading()
            return;
        }
        this.setData({
            'form.areaCode': data.regionCode
        })
        this.getStreet(data.regionCode)
    },
    // 获取街道
    async getStreet(code) {
        const res = await reqRegionLevel(code)
        if (res.code !== 20000) return showMsg(res.message)
        let {
            rows
        } = res.data
        this.setData({
            streetList: rows,
            showStreet: true,
            'form.street': "",
            'form.community': "",
        })
        await this.geocoder(this.data.form.province + this.data.form.city + this.data.form.area + this.data.form.street)
        wx.hideLoading()
    },

    // 弹出省
    showprovinceClick() {
        this.setData({
            showprovince: true
        })
    },
    // 关闭省弹窗
    provinceClose() {
        this.setData({
            showprovince: false
        })
    },
    // 弹出市
    showcityClick() {
        this.setData({
            showcity: true
        })
    },
    // 关闭市弹窗
    cityClose() {
        this.setData({
            showcity: false
        })
    },
    // 弹出区
    showareaClick() {
        this.setData({
            showarea: true
        })
    },
    // 关闭区弹窗
    areaClose() {
        this.setData({
            showarea: false
        })
    },

    // 关闭街道弹窗
    streetClose() {
        this.setData({
            showStreet: false
        })
    },

    // 弹出街道
    showStreetClick() {
        this.setData({
            showStreet: true
        })
    },

    // 关闭社区弹窗
    communityClose() {
        this.setData({
            showCommunity: false
        })
    },
    // 弹出社区
    showCommunityClick() {
        this.setData({
            showCommunity: true
        })
    },
    // 省确认
    provinceConfirm(e) {
        console.log(e);
        let location = {
            province: e.detail.value.regionName,
        }
        this.setData({
            "form.province": e.detail.value.regionName, //省
            showprovince: false
        })
        this.getProvince(location)
    },
    // 市确认
    cityConfirm(e) {
        let location = {
            city: e.detail.value.regionName,
        }
        this.setData({
            "form.city": e.detail.value.regionName, //市
            showcity: false
        })
        this.getCity(location, this.data.form.provinceCode)
    },
    // 区确认
    areaConfirm(e) {
        let location = {
            district: e.detail.value.regionName,
        }
        this.setData({
            "form.area": e.detail.value.regionName, //区
            'form.street': "",
            showarea: false
        })
        this.getArea(location, this.data.form.cityCode)
    },
    // 点击确认街道
    async streetConfirm(e) {
        console.log(e);
        const {
            value,
        } = e.detail;
        this.setData({
            'form.streetCode': value.regionCode,
            'form.street': value.regionName,
            'form.communityCode': '',
            'form.community': '',
        })
        this.streetClose()
        // 获取社区列表
        const res = await reqRegionLevel(value.regionCode)
        if (res.code !== 20000) return showMsg(res.message)
        let {
            rows
        } = res.data
        this.setData({
            communityList: rows,
        })
        await this.geocoder(this.data.form.province + this.data.form.city + this.data.form.area + this.data.form.street)
    },

    async communityConfirm(e) {
        console.log(e);
        const {
            value,
        } = e.detail;
        this.setData({
            'form.communityCode': value.regionCode,
            'form.community': value.regionName,
        })
        this.communityClose()
    },

    onUnload() {
        // 页面卸载时设置插件选点数据为null，防止再次进入页面，geLocation返回的是上次选点结果
        chooseLocation.setLocation(null);
    },
})